package ru.compscicenter.uno.wizard;

import ru.compscicenter.uno.GameSettings;
import ru.compscicenter.uno.log.Logger;
import ru.compscicenter.uno.net.TCPServer;
import ru.compscicenter.uno.util.ImagePool;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * User: SLeeP
 * Date: 03.03.12
 * Time: 4:35
 */

public class Panel1 extends JPanel {
    public final static String IS_SERVER = "Server";
    public final static String IS_CLIENT = "Client";

    private javax.swing.ButtonGroup roleGroup;
    private static javax.swing.JRadioButton serverRadioButton;
    private javax.swing.JRadioButton clientRadioButton;
    private JLabel blankSpace;
    private JLabel blankSpace2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    public  JLabel networkInterfaceLabel;
    private static JTextField nameTextField;
    private static JComboBox<Object> networkInterfaceBox;

    private JLabel welcomeTitle;
    private JPanel contentPanel;

    private JLabel iconLabel;
    private ImageIcon icon;
    private GameSettings gameSettings;

    
    public Panel1(GameSettings gameSettings) {
        this.gameSettings = gameSettings;

        iconLabel = new JLabel();
        contentPanel = getContentPanel();
        contentPanel.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));

        icon = new ImageIcon(ImagePool.getImage(Wizard.unoIconDIR, 250, 383));

        setLayout(new java.awt.BorderLayout());

        if (icon != null)
            iconLabel.setIcon(icon);

        iconLabel.setBorder(new EtchedBorder(EtchedBorder.RAISED));

        add(iconLabel, BorderLayout.WEST);

        JPanel secondaryPanel = new JPanel();
        secondaryPanel.add(contentPanel, BorderLayout.NORTH);
        add(secondaryPanel, BorderLayout.CENTER);
    }

    public static String getPlayerName() {
        return nameTextField.getText();
    }
    
    public static String getMode() {
        if (serverRadioButton.isSelected()) {
            return IS_SERVER;
        } else {
            return IS_CLIENT;
        }
    }

    public void addNameListener(DocumentListener dl) {
        nameTextField.getDocument().addDocumentListener(dl);
    }

    public static String getNetworkInterface() {
        return networkInterfaceBox.getSelectedItem().toString();
    }

    public String getRadioButtonSelected() {
        System.out.println(roleGroup.getSelection().getActionCommand());
        return roleGroup.getSelection().getActionCommand();
    }

    public void addRadioButtonChangeListener(ActionListener l) {
        serverRadioButton.addActionListener(l);
        clientRadioButton.addActionListener(l);
    }

    private JPanel getContentPanel() {

        roleGroup = new javax.swing.ButtonGroup();
        JPanel contentPanel1 = new JPanel();
        JPanel jPanel1 = new JPanel();

        welcomeTitle = new JLabel();
        blankSpace = new JLabel();
        blankSpace2 = new JLabel();
        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        networkInterfaceBox = new JComboBox<Object>(TCPServer.getNetworkInterface().toArray());
        gameSettings.networkInterface = networkInterfaceBox.getItemAt(0).toString();
        networkInterfaceLabel = new JLabel();

        nameTextField = new JTextField();
        serverRadioButton = new javax.swing.JRadioButton();
        clientRadioButton = new javax.swing.JRadioButton();

        serverRadioButton.setActionCommand(IS_SERVER);
        clientRadioButton.setActionCommand(IS_CLIENT);
        serverRadioButton.setSelected(true);
        gameSettings.mode = IS_SERVER;
        contentPanel1.setLayout(new java.awt.BorderLayout());
        addChangeListener();
        welcomeTitle.setFont(new java.awt.Font("MS Sans Serif", Font.BOLD, 14));
        welcomeTitle.setText("Welcome to UNO game!!");
        contentPanel1.add(welcomeTitle, java.awt.BorderLayout.NORTH);

        jPanel1.setLayout(new java.awt.GridLayout(0, 1));

        jPanel1.add(blankSpace);

        jLabel2.setText("Please, enter your name: ");
        jPanel1.add(jLabel2);

        jPanel1.add(nameTextField);
        jPanel1.add(blankSpace2);
        jLabel1.setText("Choose your role in this application: ");
        jPanel1.add(jLabel1);

        serverRadioButton.setText("Server");
        roleGroup.add(serverRadioButton);
        jPanel1.add(serverRadioButton);

        clientRadioButton.setText("Client");
        roleGroup.add(clientRadioButton);
        jPanel1.add(clientRadioButton);

        networkInterfaceLabel.setText("Choose your external IP from a list: ");
        jPanel1.add(networkInterfaceLabel);
        jPanel1.add(networkInterfaceBox);

        contentPanel1.add(jPanel1, java.awt.BorderLayout.CENTER);
        return contentPanel1;
    }


    private void addChangeListener() {
        networkInterfaceBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                gameSettings.networkInterface = networkInterfaceBox.getSelectedItem().toString();
            }
        });
    }
}
